package com.kingdee.eas.hr.affair.client;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.ctrl.extendcontrols.KDBizPromptBox;
import com.kingdee.bos.ctrl.kdf.table.IColumn;
import com.kingdee.bos.ctrl.kdf.table.IRow;
import com.kingdee.bos.ctrl.kdf.table.KDTable;
import com.kingdee.bos.dao.IObjectValue;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.bos.util.BOSObjectType;
import com.kingdee.eas.base.permission.FieldAccessInfo;
import com.kingdee.eas.base.permission.FieldPermission;
import com.kingdee.eas.base.permission.IPermission;
import com.kingdee.eas.base.permission.PermissionFactory;
import com.kingdee.eas.base.permission.UserInfo;
import com.kingdee.eas.basedata.org.HROrgUnitInfo;
import com.kingdee.eas.basedata.org.OrgType;
import com.kingdee.eas.basedata.org.PositionInfo;
import com.kingdee.eas.basedata.person.PersonInfo;
import com.kingdee.eas.common.client.SysContext;
import com.kingdee.eas.fdc.basedata.FDCSQLBuilder;
import com.kingdee.eas.hr.affair.EmployeePoolInfo;
import com.kingdee.eas.hr.affair.LoanInBizBillEntryInfo;
import com.kingdee.eas.hr.affair.LoanOutBizBillEntryCollection;
import com.kingdee.eas.hr.affair.LoanOutBizBillEntryFactory;
import com.kingdee.eas.hr.affair.LoanOutBizBillEntryInfo;
import com.kingdee.eas.hr.cifihr.BelongFactory;
import com.kingdee.eas.hr.cifihr.BelongInfo;
import com.kingdee.eas.hr.cifihr.DispatchAddressFactory;
import com.kingdee.eas.hr.cifihr.DispatchAddressInfo;
import com.kingdee.eas.hr.cifihr.DispatchTypeFactory;
import com.kingdee.eas.hr.cifihr.DispatchTypeInfo;
import com.kingdee.eas.hr.emp.IPersonLoan;
import com.kingdee.eas.hr.emp.LoanStrEnum;
import com.kingdee.eas.hr.emp.PersonLoanCollection;
import com.kingdee.eas.hr.emp.PersonLoanFactory;
import com.kingdee.eas.hr.emp.PersonLoanInfo;
import com.kingdee.eas.hr.emp.PersonPositionInfo;
import com.kingdee.eas.rpts.ctrlsqldesign.client.dispatch.DispatchF7ParamPicker;
import com.kingdee.eas.util.client.EASResource;
import com.kingdee.eas.util.client.MsgBox;
import com.kingdee.jdbc.rowset.IRowSet;

public class LoanInBizBillEditUIBizCTEx extends LoanInBizBillEditUIBiz {
    protected IObjectValue createNewDetailData(PersonInfo pInfo)
    {
        LoanInBizBillEntryInfo info = new LoanInBizBillEntryInfo();
        info.setPerson(pInfo);
        PersonPositionInfo ppInfo = (PersonPositionInfo)pInfo.get("personPosition");
        if(ppInfo != null)
        {
            PositionInfo pstInfo = ppInfo.getPrimaryPosition();
            info.setOldJobGrade(ppInfo.getJobGrade());
            info.setOldJobLevel(ppInfo.getJobLevel());
            info.setOldPosition(ppInfo.getPrimaryPosition());
            info.setOldAdminOrg(ppInfo.getPersonDep());
            if(ppInfo.getPrimaryPosition() != null)
                info.setOldJob(ppInfo.getPrimaryPosition().getJob());
            info.setInAdminStr(true);
        }
        return info;
    }
    public void addLine(boolean isInsert)
    throws Exception
{
    addEmpPoolF7(isInsert);
}

protected void addEmpPoolF7(boolean isInsert)
    throws Exception
{
    KDBizPromptBox f7 = new KDBizPromptBox();
    f7.setQueryInfo("com.kingdee.eas.hr.affair.app.EmployeePoolListQuery");
    FilterInfo filter = new FilterInfo();
    filter.getFilterItems().add(new FilterItemInfo("source", new Integer(2), CompareType.EQUALS));
    HROrgUnitInfo hro = (HROrgUnitInfo)ctx.getMainOrgContext().get(OrgType.HRO);
    if(hro == null)
    {
        MsgBox.showError(EASResource.getString("com.kingdee.eas.hr.affair.AFFAIRAutoGenerateResource", "55_FlucInBizBillEditUIBiz"));
        return;
    }
    filter.getFilterItems().add(new FilterItemInfo("targetPool.id", hro.getId(), CompareType.EQUALS));
    filter.getFilterItems().add(new FilterItemInfo("targetPool.id", null, CompareType.EQUALS));
    filter.setMaskString("#0 and (#1 or #2)");
    EntityViewInfo ev = new EntityViewInfo();
    ev.setFilter(filter);
    f7.setEntityViewInfo(ev);
    f7.setEnabledMultiSelection(true);
    f7.setDataBySelector();
    if(!f7.getSelector().isCanceled())
    {
        List persons = new ArrayList();
        String personIds = "";
        Map employeePoolMap = new HashMap();
        if(f7.getData() instanceof Object[])
        {
            Object objs[] = (Object[])f7.getData();
            for(int i = 0; i < objs.length; i++)
            {
                EmployeePoolInfo empPoolInfo = (EmployeePoolInfo)objs[i];
                persons.add(empPoolInfo.getPerson());
                personIds = personIds + "'" + empPoolInfo.getPerson().getId() + "' ";
                employeePoolMap.put(empPoolInfo.getPerson().getId().toString(), empPoolInfo);
            }

        } else
        {
            EmployeePoolInfo empPoolInfo = (EmployeePoolInfo)f7.getData();
            persons.add(empPoolInfo.getPerson());
            personIds = personIds + "'" + empPoolInfo.getPerson().getId() + "' ";
            employeePoolMap.put(empPoolInfo.getPerson().getId().toString(), empPoolInfo);
        }
        addLineByPersonList(persons, true);
        personIds = personIds.trim().replaceAll(" ", ",");
        afterAddEmpPool(personIds);
    }
}

private void afterAddEmpPool(String personIds)
    throws Exception
{
    String oql = "select *,outPosition.*,outAdmin.*,outJob.*,outJobLevel.*,outJobGrade.* where state='1' person.id in (" + personIds + ")";
    IPersonLoan ipersonLoan = PersonLoanFactory.getRemoteInstance();
    PersonLoanCollection col = ipersonLoan.getPersonLoanCollection(oql);
    Map personLoanMap = new HashMap();
    PersonLoanInfo personLoan;
    for(Iterator iterator = col.iterator(); iterator.hasNext(); personLoanMap.put(personLoan.getPerson().getId().toString(), personLoan))
        personLoan = (PersonLoanInfo)iterator.next();

    for(int i = 0; i < kdtEntrys.getRowCount(); i++)
    {
        IRow row = kdtEntrys.getRow(i);
        PersonInfo person = (PersonInfo)row.getCell("person").getValue();
        PersonLoanInfo personLoan1 = (PersonLoanInfo)personLoanMap.get(person.getId().toString());
        if(personLoan1 == null)
            continue;
        row.getCell("oldPosition").setValue(personLoan1.getOutPosition());
        row.getCell("oldAdminOrg").setValue(personLoan1.getOutAdmin());
        
        row.getCell("oldJob").setValue(personLoan1.getOutJob());
        row.getCell("oldJobLevel").setValue(personLoan1.getOutJobLevel());
        row.getCell("oldJobGrade").setValue(personLoan1.getOutJobGrade());
        row.getCell("owner").setValue(personLoan1.getOwner());
        row.getCell("outPositionStr").setValue(new Boolean(false));
        row.getCell("outAdminStr").setValue(new Boolean(false));
        if(LoanStrEnum.OUT.equals(personLoan1.getPositionStr()))
            row.getCell("outPositionStr").setValue(new Boolean(true));
        if(LoanStrEnum.OUT.equals(personLoan1.getAdminStr()))
            row.getCell("outAdminStr").setValue(new Boolean(true));
        row.getCell("loanEndDate").setValue(personLoan1.getLoanEndDate());
     	EntityViewInfo View = new EntityViewInfo();
	    FilterInfo filter = new FilterInfo();
	    filter.getFilterItems().add(new FilterItemInfo("person.id",person.getId().toString()));
    	filter.getFilterItems().add(new FilterItemInfo("oldAdminOrg.id",personLoan1.getOutAdmin().getId().toString()));
		View.setFilter(filter);
			LoanOutBizBillEntryCollection  EntryColl =  LoanOutBizBillEntryFactory.getRemoteInstance().getLoanOutBizBillEntryCollection(View);
			 if(EntryColl.size()>0){
		   	        LoanOutBizBillEntryInfo entryInfo =EntryColl.get(0);
		   	    	FDCSQLBuilder sql = new FDCSQLBuilder();
				    sql.appendSql("select * from T_HR_LoanOutBizBillEntry where fid='"+ entryInfo.getId().toString() + "'");
		   	        String oldBlongID=null; 
		   	        String newBlongID=null;
		   	        String dispatchTypeID=null;
		   	        String dispatchAdressID=null;
		   	        String oldcompanyID=null;
		   	 	    IRowSet rowset = sql.executeQuery();
		   	 	   while(rowset.next()){
		   	 		 oldBlongID = rowset.getString("cfoldBlongID");
		   	 		 newBlongID = rowset.getString("cfnewBlongID");
		   	 		 dispatchTypeID = rowset.getString("cfdispatchTypeId");
		   	 		 dispatchAdressID = rowset.getString("cfdispatchAdressId");
		   	 	     break;
		   	 	   }
				    if(oldBlongID!=null){
				    	BelongInfo oldBInfo = BelongFactory.getRemoteInstance().getBelongInfo(new ObjectUuidPK(oldBlongID));
						row.getCell("oldBlongs").setValue(oldBInfo);
		   	        }
				    if(newBlongID!=null){
				    	BelongInfo newBInfo = BelongFactory.getRemoteInstance().getBelongInfo(new ObjectUuidPK(newBlongID));
						row.getCell("newBlongs").setValue(newBInfo);
		   	        }
		   	        if(dispatchTypeID!=null){
		   	        	DispatchTypeInfo disInfo = DispatchTypeFactory.getRemoteInstance().getDispatchTypeInfo(new ObjectUuidPK(dispatchTypeID));
		   	            row.getCell("dispatchType").setValue(disInfo);
		   	        }
		   	       if(dispatchAdressID!=null){
		   	    	   DispatchAddressInfo adressInfo = DispatchAddressFactory.getRemoteInstance().getDispatchAddressInfo(new ObjectUuidPK(dispatchAdressID));
		   	           row.getCell("dispatchAdress").setValue(adressInfo);
		   	        }
			 }
    }

}

protected boolean isAddPersonByF7()
{
    return true;
}

protected String[] getNotDupColumn()
{
    return (new String[] {
        "person"
    });
}

protected String[] getNotNullColumn()
{
    return (new String[] {
        "position", "adminOrg", "bizDate", "loanEndDate"
    });
}

protected String getPrintQuery(String dsName)
{
    getClass();
    if("baseInfo".equals(dsName))
        return "com.kingdee.eas.hr.affair.app.LoanInBizBillPrintQuery";
    getClass();
    if("approveInfo".equals(dsName))
        return "com.kingdee.eas.hr.affair.app.LoanInBizBillApprovePrintQuery";
    else
        return null;
}

protected String getPrintPath()
{
    return "/HR/AffairBiz/LoanIn";
}

public void initDataStatus()
    throws Exception
{
    super.initDataStatus();
    UserInfo userInfo = SysContext.getSysContext().getCurrentUserInfo();
    HROrgUnitInfo hrOrgInfo = SysContext.getSysContext().getCurrentHRUnit();
    IPermission per = PermissionFactory.getRemoteInstance();
    FieldPermission fields = per.getFieldPermission(BOSObjectType.create("6F8C96B2"), new ObjectUuidPK(userInfo.getId()), new ObjectUuidPK(hrOrgInfo.getId()));
    IColumn col = kdtEntrys.getColumn(getJobLevelCol());
    FieldAccessInfo accessInfo = fields.getFieldAccessInfo("entrys.jobLevel");
    if(accessInfo != null && !accessInfo.isEditable() && col != null)
        col.getStyleAttributes().setBackground(CANNOT_INPUT_COLOR);
    col = kdtEntrys.getColumn(getJobGradeCol());
    accessInfo = fields.getFieldAccessInfo("entrys.jobGrade");
    if(accessInfo != null && !accessInfo.isEditable() && col != null)
        col.getStyleAttributes().setBackground(CANNOT_INPUT_COLOR);
}
}
